﻿using KinonekoSoftware.Foundation.Platform.Graphics;

namespace KinonekoSoftware.Studio
{
    partial class Platform
    {
        


        public static async Task<Result<ImageEditFeedback>> Avatar(bool buildImage = true)
        {
            var result = await ImageEditorViewModel.Avatar();

            if (!result.IsFinished)
            {
                return Result<ImageEditFeedback>.Failed(result.Reason);
            }


            var    ier = result.Value;
            byte[] buffer;

            if (ier.Mode == EditMode.Crop)
            {
                var pip = Ioc.Get<IPlatformImageProcessor>();
                pip.Release();
                pip.Crop(ier.ResizedBuffer, ier.X, ier.Y, ier.Width, ier.Height, out buffer);
            }
            else
            {
                buffer = ier.ResizedBuffer;
            }


            var id     = ID.Get();
            var bi     = buildImage ? MediaFactory.ToBitmap(buffer) : null;

            return Result<ImageEditFeedback>.Success(
                                                     new ImageEditFeedback
                                                     {
                                                         ImageID     = id,
                                                         ImageBuffer = buffer,
                                                         Image       = bi,
                                                     });
        }

        public static async Task<Result<ImageEditFeedback>> Thumbnail()
        {
            var result = await ImageEditorViewModel.Thumbnail();

            if (!result.IsFinished)
            {
                return Result<ImageEditFeedback>.Failed(result.Reason);
            }
            var v = result.Value;

            //
            // 原始的文件
            var originBuffer    = v.OriginBuffer;
            var thumbnailBuffer = v.ResizedBuffer;

            //
            //
            // var engine = GetEngine<FileSetOfImage>();
            var id = ID.Get();
            // var bi     = MediaFactory.ToBitmap(thumbnailBuffer);
            var feedback = new ImageEditFeedback
            {
                ImageID      = id,
                ImageBuffer  = thumbnailBuffer,
                OriginBuffer = originBuffer,
                Image        = null, //bi
            };

            //
            // Debug.Assert(engine is not null);
            return Result<ImageEditFeedback>.Success(feedback);
        }

        public static async Task<Result<ImageEditFeedback>> Emoji()
        {
            var result = await ImageEditorViewModel.Emoji();

            if (!result.IsFinished)
            {
                return Result<ImageEditFeedback>.Failed(result.Reason);
            }
            var v = result.Value;

            //
            // 原始的文件
            // var originBuffer    = v.OriginBuffer;
            var thumbnailBuffer = v.ResizedBuffer;

            //
            //
            // var engine = GetEngine<FileSetOfImage>();
            var id = ID.Get();
            // var bi     = MediaFactory.ToBitmap(thumbnailBuffer);
            var feedback = new ImageEditFeedback
            {
                ImageID      = id,
                ImageBuffer  = thumbnailBuffer,
                OriginBuffer = thumbnailBuffer,
                Image        = null, //bi
            };

            //
            // Debug.Assert(engine is not null);
            return Result<ImageEditFeedback>.Success(feedback);
        }

        public static async Task<Result<ImageEditFeedback>> Icon(bool buildImage = true)
        {
            var result = await ImageEditorViewModel.Icon();

            if (!result.IsFinished)
            {
                return Result<ImageEditFeedback>.Failed(result.Reason);
            }
            var v = result.Value;

            //
            // 原始的文件
            var buffer = v.OriginBuffer;
            var id     = ID.Get();
            var bi     = buildImage ? MediaFactory.ToBitmap(buffer) : null;

            return Result<ImageEditFeedback>.Success(new ImageEditFeedback
            {
                ImageID     = id,
                ImageBuffer = buffer,
                Image       = bi,
            });
        }

        public static async Task<Result<IEnumerable<FastImageEditResult>>> Image(bool multiple = true)
        {
            
            var result = await ImageEditorViewModel.Image(multiple);

            if (!result.IsFinished)
            {
                return Result<IEnumerable<FastImageEditResult>>.Failed(result.Reason);
            }

            return Result<IEnumerable<FastImageEditResult>>.Success(result.Value);
        }
    }
}